/*
    Copyright © 2015-2019 by The qTox Project Contributors

    This file is part of qTox, a Qt-based graphical interface for Tox.

    qTox is libre software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    qTox is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with qTox.  If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "video/videomode.h"
#include "video/videosource.h"
#include "record_desktop.h"
#include <map>
#include <string>
#include <vector>
#include <atomic>
#include <thread>
#include <future>


class DesktopSource : public VideoSource
{
public:
    static DesktopSource& getInstance();
    static void destroyInstance();
    static std::vector<VideoMode> getScreenModes();


    bool isNone() const;

    // VideoSource interface
    void subscribe() override;
    void unsubscribe() override;

//public slots:
    void setupDevice(const std::string& deviceName,  VideoMode& mode);

//signals:
    sigslot::signal<> deviceOpened;
    sigslot::signal<> openFailed;

private:
    DesktopSource();
    ~DesktopSource();

//private slots:
    //void openDevice();
    // closeDevice();

    void on_desktop_data(FRAME_PKG* frame);
private:
    //std::future<int> streamFuture;

    std::string deviceName;
    VideoMode mode;

    record_desktop* _recorder_desktop;
    int videoStreamIndex;

    std::recursive_mutex   deviceMutex;
    std::mutex streamMutex;

    std::atomic_bool _isNone;
    std::atomic_int subscriptions;

    static DesktopSource* instance;
};
